package com.sunshine.lnuplus.view

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.TypedValue
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.viewpager.widget.ViewPager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.LessonDetailAdapter
import com.sunshine.lnuplus.callback.CloseLessonDetailDialogCallBack
import com.sunshine.lnuplus.callback.EditLessonCallBack
import com.sunshine.lnuplus.model.LessonBean
import kotlinx.android.synthetic.main.dialog_lesson_detail.view.*


class LessonDetailDialog : DialogFragment(){

    companion object{
        private lateinit var lessonList: List<LessonBean>
        private lateinit var callBack: EditLessonCallBack
        @JvmStatic
        fun newInstance(
            lessonList: List<LessonBean>,
            callBack: EditLessonCallBack
        ) : LessonDetailDialog{
            this.lessonList = lessonList
            this.callBack = callBack
            return LessonDetailDialog()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.dialog_lesson_detail, container, false)
        val viewPager = view.findViewById<ViewPager>(R.id.lesson_detail_view_pager)
        viewPager.apply {
            pageMargin = 50
            offscreenPageLimit = 10
            adapter = LessonDetailAdapter(childFragmentManager, lessonList, object : CloseLessonDetailDialogCallBack{
                override fun closeDialog() {
                    dialog?.dismiss()
                }
            }, callBack)
        }
        if(lessonList.size > 1){
            if(view.indicator != null){
                view.indicator.setupWithViewPager(viewPager) //竖屏指示器
            }

        }
        val params = dialog?.window?.attributes
        params?.apply {
            gravity = Gravity.BOTTOM
            windowAnimations = R.style.bottom_animation
        }
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        if(savedInstanceState != null){
            //必须开启新线程否则不能执行
            object : Thread(){
                override fun run(){
                    dialog?.dismiss()
                }
            }.start()
        }

        return view
    }

    override fun onStart() {
        super.onStart()

        //先转换dp
        var height = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            375f,
            resources.displayMetrics
        ).toInt()

        if(height > activity!!.windowManager.defaultDisplay.height){
            height = WindowManager.LayoutParams.MATCH_PARENT
        }

        dialog?.window?.setLayout(activity!!.windowManager.defaultDisplay.width, height)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putBoolean("hasData", true)
    }
}